﻿using System;
using System.Windows;
using Autofac;
using GC_FFW.Common.Modules.Autofac;
using GC_FFW.ForecastWriter.Modules.Autofac;
using GC_FFW.ForecastWriterClient.Abstractions.Interfaces.Managers;
using GC_FFW.ForecastWriterClient.Helpers;
using GC_FFW.ForecastWriterClient.Modules.Autofac;
using GC_FFW.SoccerfameParser.Modules.Autofac;

namespace GC_FFW.ForecastWriterClient
{
	/// <summary>
	/// Interaction logic for App.xaml
	/// </summary>
	public partial class App : Application
	{
		#region Variables

		private IContainer _diContainer;

		#endregion

		#region Override Methods

		protected override void OnStartup(StartupEventArgs e)
		{
			SafeOperationsManager.SafeExecute(() =>
			{
				base.OnStartup(e);

				var builder = new ContainerBuilder();

				var uri = new Uri(GetType().Assembly.GetName().CodeBase);
				builder.RegisterModule(new CommonModule(uri.LocalPath));
				builder.RegisterModule(new ForecastWriterModule());
				builder.RegisterModule(new SoccerfameParserModule());
				builder.RegisterModule(new ForecastWriterClientModule());

				_diContainer = builder.Build();

				var windowManager = _diContainer.Resolve<IWindowManager>();
				if (windowManager != null)
					windowManager.ShowForecastManagerWindow();
			});
		}

		protected override void OnExit(ExitEventArgs e)
		{
			SafeOperationsManager.SafeExecute(() =>
			{
				_diContainer.Dispose();
				base.OnExit(e);
			});
		}

		#endregion
	}
}
